<?php

namespace application\controller\cp;

use application\controller\cp\BaseController;
use application\entity\input\AdministratorSearch;
use application\service\AdministratorService;
use core\system\HttpRequest;
use application\service\FunctionService;

class AdministratorController extends BaseController {

    private $administratorService;
    private $functionService;

    public function __construct() {
        parent::__construct();
        $this->administratorService = new AdministratorService();
        $this->functionService = new FunctionService();
        $this->view->layout = '/layout/main';
    }

    /**
     * @module
     * @comment(Quản trị admin)
     * @mapping(value=cp/administrator.html)
     */
    public function grid() {
        $this->view->cache = [false, 'cp.admin', 'grid', 10000];
        $search = HttpRequest::getRequest(AdministratorSearch::class);
        if (!isset($search->pageIndex) || $search->pageIndex < 0) {
            $search->pageIndex = 1;
        }
        $search->pageSize = 20;
        $this->view->search = $search;
        $this->view->dataPage = $this->administratorService->search($search);
        $this->view->scriptClient = "var cpFunctionGroups = " . json_encode($this->functionService->getCpFunctionGroup())
                . "; var cpFunctions = " . json_encode($this->functionService->getCpFunctions())
                . ";administrator.init();";
        $this->view->render('/administrator/grid');
    }

}
